
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "include/parser.h"
#include "include/acsLogger.h"



char* strip( char* str, char* tag)
{
	char* ret_str = malloc(strlen(str) - strlen(tag));
	memset(ret_str,0,(strlen(str) - strlen(tag)));
	
	strncpy(ret_str,str,find_string(str,tag,0));
	
					strcat(ret_str,
			               substring(str,tag,find_string(str,tag,0) + strlen(tag),strlen(str)) );
	    	
    	return ret_str;    
}
/**
  Function Name     : stripTag 
  Input Paramaters  : String which contained XML and tag name
  Output Paramaters : char pointer 
  Result            : string without tags 
  Description       : striping tags from string
*/

char* stripTag( char* str,const char* tag)
{
	char* ret_str = malloc(strlen(str) - strlen(tag) - 3);
	memset(ret_str,0,(strlen(str) - strlen(tag) - 3));
	char* begin_tmp = "<";
        char* begin_tag = malloc(strlen(begin_tmp) + strlen(tag) + 1);
	memset(begin_tag,0,strlen(begin_tmp) + strlen(tag) + 1);
	char* end_tmp1 = "</";
	char* end_tmp2 = ">";
 	char* end_tag = malloc(strlen(end_tmp1) + strlen(tag) + strlen(end_tmp2) + 1);
	memset(end_tag,0,strlen(end_tmp1) + strlen(tag) + strlen(end_tmp2) + 1);

 	strcpy(begin_tag,begin_tmp);
	strcat(begin_tag,tag);
	
	
	strcpy(end_tag,end_tmp1);
	strcat(end_tag,tag);
	strcat(end_tag,end_tmp2);
	
         size_t begin = find_string(str, ">",0);
	 str = substring(str, str,begin + 1, strlen(str) - 1);
	 strncpy(ret_str,str,strlen(str) - strlen(tag) - 3);
		
	 if(begin_tag)
        free(begin_tag);
	if(end_tag)
	free(end_tag);

	
    	return ret_str;
}

/**
  Function Name     : getTags 
  Input Paramaters  : char pointer fContent and tag, int position 
  Output Paramaters : char pointer
  Result            : char pointer ret_val
  Description       : get tags from string beginning from position
 */
char* getTags( char *fContent, char* tag,int pos )//?????????????????????????????????????????
{
	
	char* ret_val = malloc(128);
 	memset(ret_val,0,128);
	size_t len = strlen(tag);
	char* begin_tmp = "<";
        char* begin_tag = (char*)malloc(strlen(begin_tmp) + strlen(tag) + 1);
	memset(begin_tag,0,strlen(begin_tmp) + strlen(tag) + 1);
	char* end_tmp1 = "</";
	char* end_tmp2 = ">";
 	char* end_tag = (char*)malloc(strlen(end_tmp1) + strlen(tag) + strlen(end_tmp2) + 1);
	memset(end_tag,0,strlen(end_tmp1) + strlen(tag) + strlen(end_tmp2) + 1);

 	strcpy(begin_tag,begin_tmp);
	strcat(begin_tag,tag);
	
	strcpy(end_tag,end_tmp1);
	strcat(end_tag,tag);
	strcat(end_tag,end_tmp2);
	
	char* buf = (char*)malloc(512);
	memset(buf,0,512);
	size_t begin = find_string(fContent, begin_tag, 0);
	size_t end = find_string(fContent, end_tag, begin);

	while( end != NOT_FOUND )
	{
		buf = substring(fContent, buf, begin, end - begin + len + 3);
		if( pos == 1 )
		{
		  strcpy(ret_val,stripTag(buf,tag));
		  
		}
		else if( pos == 0 )
		{
		 strcat(ret_val,stripTag(buf,tag));
		 strcat(ret_val,"\n");
		}
		
		if(find_string(ret_val,"&#10;",0) !=  -1)
        {
				strcpy(ret_val,strip(ret_val,"&#10;"));
		}
		
		begin = end + len + 3;
		begin = find_string(fContent,begin_tag, begin);
		if( begin == NOT_FOUND )
			break;
		end = begin;
		end = find_string(fContent,end_tag, end);
		
	}
	 if(begin_tag)
        free(begin_tag);
	if(end_tag)
	free(end_tag);
	if(buf)
	free(buf);
	return (char*)ret_val;
}

/** 
  Function Name     : find_string 
  Input Paramaters  : char pointer str and sub, int position
  Output Paramaters : integer position
  Result            : integer position 
  Description       :  find substring in string ang return position(similar C++ find)
 */
int find_string (char* str, char* sub, int pos) 
{ 
	char* pch = strstr(str+pos, sub); 
	if( pch ) 
		return (pch - str); 
	return NOT_FOUND; 
} 

/**
  Function Name     : substring 
  Input Paramaters  : char pointer from and to, int start and end
  Output Paramaters : char pointer to
  Result            : char pointer to
  Description       :  found in string substring and returned it
*/
char* substring(char* from, char* to, int start, int end)
{
	to = (char*)malloc(end+1);
	memset(to,0,end+1);
	return strncpy(to, from + start, end);
}

/**
  Function Name     : substring 
  Input Paramaters  : char array strContent and cSep
  Output Paramaters : NONE
  Result            : tokenization strContent
  Description       :  tokenize string
 */
void tokenization(char strContent[], char cSep[])
{
  int i;
  for(i = 0;i < 4;i++)
    memset(mystring[i].str,0,512);  
  
        strSz = 0;
	char *token = malloc(512);
	memset(token,0,512);
	token = "";
	token = strtok(strContent, cSep);
	size_t nIndex=0;
	while (token != NULL) {
		strcpy(mystring[nIndex].str,token);
		token = strtok(NULL, cSep);
		nIndex++;
	}
	strSz = nIndex;
	if(token)
	free(token);
}

/**
  Function Name     : toUpperCase 
  Input Paramaters  : char pointer str
  Output Paramaters : char pointer str
  Result            : toUpperCase str
  Description       :  takes a string argument and returns a  UpperCase string
 */

char* toUpperCase(char* str)
{
  int i = 0;
  for(i=0; i < strlen(str); i++)
    str[i] = toupper(str[i]);
  return str;
}

/**
  Function Name     : fileRead 
  Input Paramaters  : char pointer fName
  Output Paramaters : char pointer buffer
  Result            : string that contains the contents of the file
  Description       :  read file
 */

char* fileRead(const char* fName)
{
  FILE *f;
 char* buffer = (char*)malloc(512);
 memset(buffer,0,512);
 f = fopen(fName, "rb");
  if (f)
    fread(buffer, 512, 1, f);
  else
    printf("error opening file");
  
    fclose(f);
    return buffer;
}

/**
  Function Name     : initDBaseParameters 
  Input Paramaters  : const char pointer fName, char pointer server,user, password, database
  Output Paramaters : NONE
  Result            : init DB parameters(server,user, password, database)
  Description       : init DB parameters from config file
*/

void initDBaseParameters(const char* fName, char* server, char* user,char* password, char* database)
{
  int pos = 0;

    tokenization((char*)fileRead(fName),"\n");
   
    pos = find_string(mystring[0].str," ",0);
    strncpy(server,mystring[0].str,pos);

    pos = find_string(mystring[1].str," ",0);
    strncpy(user,mystring[1].str,pos);

    pos = find_string(mystring[2].str," ",0);
    strncpy(password,mystring[2].str,pos);

    pos = find_string(mystring[3].str," ",0);
    strncpy(database,mystring[3].str,pos);
}

/**
  Function Name     : getTagElems 
  Input Paramaters  : char  pointer fContent and tag
  Output Paramaters : char pointer tag
  Result            : get tags 
  Description       : get tag elements from file 
*/
char* getTagElems(char *fContent,const char* tag)
{
        char* ret_val = malloc(4096);
	memset(ret_val,0,4096);
        size_t len = strlen(tag);
        char* begin_tmp = "<";
        char* begin_tag = malloc(strlen(begin_tmp) + strlen(tag) + 1);
	memset(begin_tag,0,strlen(begin_tmp) + strlen(tag) + 1 );
        char* end_tmp1 = "</";
        char* end_tmp2 = ">";
        char* end_tag = malloc(strlen(end_tmp1) + strlen(tag) + strlen(end_tmp2) + 1);
	memset(end_tag,0,strlen(end_tmp1) + strlen(tag) + strlen(end_tmp2) + 1 );
	
        strcpy(begin_tag,begin_tmp);
        strcat(begin_tag,tag);

        strcpy(end_tag,end_tmp1);
        strcat(end_tag,tag);
        strcat(end_tag,end_tmp2);

        char* buf = malloc(512);
	memset(buf,0,512);
        size_t begin = find_string(fContent, begin_tag, 0);
        size_t end = find_string(fContent, end_tag, begin);
        
        while( end != NOT_FOUND )
        {
                buf = substring(fContent, buf, begin, end - begin + len + 3);
                int p = find_string(buf,"xsd:string\"></",0);
                strcat(ret_val,stripTag(buf,tag));
                if( p > 0 )
                {
                  strcat(ret_val,"\n");
                  strcat(ret_val,"NONE");
		  
                }
                
                strcat(ret_val,"\n");
                
                if(find_string(ret_val,"&#10;",0) !=  -1)
                {
					strcpy(ret_val,strip(ret_val,"&#10;"));
					
				}
                
                begin = end + len + 3;
                begin = find_string(fContent,begin_tag, begin);
                if( begin == NOT_FOUND )
                        break;
                end = begin;
                end = find_string(fContent,end_tag, end);
        }
        if(begin_tag)
        free(begin_tag);
	if(end_tag)
	free(end_tag);
	if(buf)
	free(buf);
	
        return (char*)ret_val;
}


/**
  Function Name     : getValues 
  Input Paramaters  : array of struct getValue get,char pointer fileCont
  Output Paramaters : NONE
  Result            : get values 
  Description       : get values from  array of struct getValue get
*/
void getValues( getParamValues get_val,char* fileCont )
{  
  number_of_values = 0;
  char* buf = fileCont;
  char* strContent = getTagElems(buf,"Value");
  char cSep[] = "\n";
  char *token = malloc(512);
  memset(token,0,512);
	token = strtok(strContent, cSep);
	
	size_t nIndex=0;
	
	while (token != NULL) {
		memset(get_val[nIndex].getValueStr,0,sizeof(get_val[nIndex].getValueStr));
		strncpy(get_val[nIndex].getValueStr,token,strlen(token));
		nIndex++;
		token = strtok(NULL,cSep);
		//logtr(get_val[nIndex].getValueStr,"getValueStr", INFO, conferr);
	}
	number_of_values = nIndex;	
	if(token)
	free(token);
}

void getNames( getParamNames get_names,char* fileCont )
{
  number_of_names = 0;
  char* buf = fileCont;
  char* strContent = getTagElems(buf,"Name");
  
  char cSep[] = "\n";

  char *token = malloc(512);
  memset(token,0,512);
	token = strtok(strContent, cSep);
	size_t nIndex = 0;
	while (token != NULL) {
		memset(get_names[nIndex].getNameStr,0,sizeof(get_names[nIndex].getNameStr));
		strncpy(get_names[nIndex].getNameStr,token,strlen(token));
		nIndex++;
		token = strtok(NULL, cSep);
		//logtr(get_names[nIndex].getNameStr,"getNameStr", INFO, conferr);
	}
	
	number_of_names = nIndex;
	if(token)
	free(token);
}

int get_index(getParamNames get,char* name)
{
  int ret_val = 0,i;
	 for(i = 0;i < number_of_names; i++)
	 {
	   if(strcmp(name,get[i].getNameStr) == 0)
	   {   
	     ret_val = i;
	     break;    
	   }
	 }
  
  return ret_val;
}
